package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.R;
import com.itheima.pojo.ShoppingCart;
import com.itheima.pojo.User;
import com.itheima.service.ShoppingCartService;
import com.itheima.service.UserService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.beans.Beans;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/11/6 14:17
 */

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private UserService userService;

    /**
     * 购物车获取
     *
     * @return
     */
    @GetMapping("/list")
    public R shoppingCartList(HttpSession session) {
        Long userid = (Long) session.getAttribute("user");

        LambdaQueryWrapper<ShoppingCart> querryWrapper = new LambdaQueryWrapper<>();
        querryWrapper.eq(ShoppingCart::getUserId, userid);
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(querryWrapper);
        return R.success(shoppingCarts);
    }

    /**
     * 购物车添加
     *
     * @param shoppingCart
     * @param session
     * @return
     */
    @PostMapping("/add")
    public R addShoppingCart(@RequestBody ShoppingCart shoppingCart, HttpSession session) {
        Long userId = (Long) session.getAttribute("user");
        shoppingCart.setUserId(userId);
        // 根据用户id和菜品|套餐id查询
        LambdaQueryWrapper<ShoppingCart> querryWrapper = new LambdaQueryWrapper<>();
        querryWrapper.eq(ShoppingCart::getUserId, userId);
        querryWrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        querryWrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart shopCartOne = shoppingCartService.getOne(querryWrapper);
        if (shopCartOne == null) {
            // 没有查询到就直接添加
            shoppingCart.setUserId(userId);
            shoppingCartService.save(shoppingCart);
        } else {
            // 查询到菜品就修改数量
            shopCartOne.setNumber(shopCartOne.getNumber() + 1);
            shoppingCartService.updateById(shopCartOne);
        }
        return R.success("添加成功！");
    }

    /**
     * 购物车单个删除
     *
     * @return
     */
    @PostMapping("/sub")
    public R subShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> querryWrapper = new LambdaQueryWrapper<>();
        // 菜品
        querryWrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        // 套餐
        querryWrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart shopping = shoppingCartService.getOne(querryWrapper);
        if (shopping.getNumber() == 1) {
            shoppingCartService.removeById(shopping.getId());
            return R.success("删除成功");
        } else {
            ShoppingCart newShoppingCart = new ShoppingCart();
            newShoppingCart.setNumber(shopping.getNumber() - 1);
            newShoppingCart.setId(shopping.getId());
            shoppingCartService.updateById(newShoppingCart);
            return R.success("删除成功！");
        }

    }

    /**
     * 清空购物车
     *根据用户id清空改用户的添加记录
     * @return
     */
    @DeleteMapping("/clean")
//    @Update("truncate table shoppingCart")
    public R clearShoppingCart(HttpSession session) {
        // delete from shoppingCart;
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> querryWrapper = new LambdaQueryWrapper<>();
        querryWrapper.eq(ShoppingCart::getUserId, userId);
        shoppingCartService.remove(querryWrapper);
        return R.success("清除出成功！");
    }
}
